package com.android.memoprendiz.model;

import java.util.Vector;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class RankingDAO {
	
	Database dbManager;

	public RankingDAO(Context context){
		this.dbManager = new Database(context);
	}
	
	public Vector<Ranking> getRanking(int limit){
			
			Vector<Ranking> punctuation = new Vector<Ranking>();
		
			SQLiteDatabase database = dbManager.getReadableDatabase(); 
			
			Cursor results = database.rawQuery("SELECT * FROM pontuacao ORDER BY pontos DESC LIMIT " + limit, null);
			
			results.moveToFirst();
			if(results.getCount() > 0){
				do{
					punctuation.add(new Ranking(results.getString(1), results.getInt(2)));
				}while(results.moveToNext());
			}
			
			results.close();
			database.close();
			
			return punctuation;
	}
	
	public void salveRanking(Ranking punctuation){
		
		SQLiteDatabase database = dbManager.getWritableDatabase(); 
		database.execSQL("INSERT INTO pontuacao VALUES ( null, '" + punctuation.getPlayerName() +  "', " +
				punctuation.getPunctuation() + ");");
		
		database.close();
	}
	
	public Ranking getBestPunctuation(){
		Vector <Ranking> ranking = new Vector<Ranking>();
		ranking = this.getRanking(1);
		if(ranking.size() == 0){
			return new Ranking("", 0);
		}else{
			return ranking.get(0);
		}
	}
}
